<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-master-table tb-absolute-fill">
  <div class="tb-form-panel no-border no-padding padding-top">
    <div class="tb-form-hint tb-primary-fill tb-flex center">{{ 'gateway.hints.modbus-master' | translate }}</div>
  </div>
  <div class="tb-master-table-content flex flex-col">
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="textSearchMode">
      <div class="mat-toolbar-tools" *ngIf="(dataSource.isEmpty() | async) === false">
        <div class="title-container">
          <span class="tb-master-table-title">{{ 'gateway.servers-slaves' | translate}}</span>
        </div>
        <span class="flex-1"></span>
        <button mat-icon-button
                (click)="manageSlave($event)"
                matTooltip="{{ 'action.add' | translate }}"
                matTooltipPosition="above">
          <mat-icon>add</mat-icon>
        </button>
        <button mat-icon-button
                (click)="enterFilterMode()"
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="!textSearchMode">
      <div class="mat-toolbar-tools">
        <button mat-icon-button
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
        <mat-form-field class="flex-1">
          <mat-label>&nbsp;</mat-label>
          <input #searchInput matInput
                 [formControl]="textSearch"
                 placeholder="{{ 'common.enter-search' | translate }}"/>
        </mat-form-field>
        <button mat-icon-button (click)="exitFilterMode()"
                matTooltip="{{ 'action.close' | translate }}"
                matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <div class="table-container">
      <table mat-table [dataSource]="dataSource">
        <ng-container [matColumnDef]="'deviceName'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            <div tbTruncateWithTooltip>{{ 'gateway.device-name' | translate }}</div>
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['deviceName'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'info'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            {{ 'gateway.info' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['host'] ?? slave['port'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'unitId'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            {{ 'gateway.unit-id' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            <div tbTruncateWithTooltip>{{ slave['unitId'] }}</div>
          </mat-cell>
        </ng-container>
        <ng-container [matColumnDef]="'type'">
          <mat-header-cell *matHeaderCellDef class="table-value-column">
            <div>{{ 'gateway.type' | translate }}</div>
          </mat-header-cell>
          <mat-cell *matCellDef="let slave" class="table-value-column">
            {{ ModbusProtocolLabelsMap.get(slave['type']) }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="actions" stickyEnd>
          <mat-header-cell *matHeaderCellDef>
            <div class="gt-md:!hidden" style="width: 48px; min-width: 48px; max-width: 48px;"></div>
            <div class="lt-lg:!hidden" [style]="{ minWidth: '96px', textAlign: 'center'}"></div>
          </mat-header-cell>
          <mat-cell *matCellDef="let slave; let i = index">
            <ng-template #rowActions>
              <button mat-icon-button
                      matTooltip="{{ 'action.edit' | translate }}"
                      matTooltipPosition="above"
                      (click)="manageSlave($event, i)">
                <tb-icon>edit</tb-icon>
              </button>
              <button mat-icon-button
                      matTooltip="{{ 'action.delete' | translate }}"
                      matTooltipPosition="above"
                      (click)="deleteSlave($event, i)">
                <tb-icon>delete</tb-icon>
              </button>
            </ng-template>
            <div class="flex flex-1 flex-row items-stretch justify-end lt-lg:!hidden"
                 [style]="{ minWidth: '96px', textAlign: 'center'}">
              <ng-container [ngTemplateOutlet]="rowActions"></ng-container>
            </div>
            <div class="gt-md:!hidden">
              <button mat-icon-button
                      (click)="$event.stopPropagation()"
                      [matMenuTriggerFor]="cellActionsMenu">
                <mat-icon class="material-icons">more_vert</mat-icon>
              </button>
              <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                <ng-container [ngTemplateOutlet]="rowActions"></ng-container>
              </mat-menu>
            </div>
          </mat-cell>
        </ng-container>
        <mat-header-row class="mat-row-select" *matHeaderRowDef="['deviceName', 'info', 'unitId', 'type', 'actions']; sticky: true"></mat-header-row>
        <mat-row *matRowDef="let slave; columns: ['deviceName', 'info', 'unitId', 'type', 'actions']"></mat-row>
      </table>
      <section [class.!hidden]="textSearchMode || (dataSource.isEmpty() | async) === false"
               class="mat-headline-5 tb-absolute-fill tb-add-new items-center justify-center">
        <button mat-button class="connector"
                (click)="manageSlave($event)">
          <mat-icon class="tb-mat-96">add</mat-icon>
          <span>{{ 'gateway.add-slave' | translate }}</span>
        </button>
      </section>
    </div>
    <span [class.!hidden]="!textSearchMode || (dataSource.isEmpty() | async) === false"
          class="no-data-found items-center justify-center" translate>
      widget.no-data-found
    </span>
  </div>
</div>
